package com.kj.xuyuan.home;

import android.content.Context;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.RemindMsgAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.RemindMsgBean;
import com.kj.xuyuan.model.UserInfoBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 消息列表  评论动态之类的消息
 */
public class MsgListActivity extends BaseActivity {

    @BindView(R.id.rv_msg_list)
    RecyclerView rvMsgList;
    @BindView(R.id.sml_msg_list)
    SmartRefreshLayout smlMsgList;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private List<RemindMsgBean> data;
    private RemindMsgAdapter adapter;
    private int page = 1;

    public static void actionStar(Context context) {
        Intent intent = new Intent(context, MsgListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_msg_list;
    }

    @Override
    protected void initView() {
        setTitleText("消息列表");

        initRecyclerView();
        getMsg();
    }

    private void getMsg() {
        Map map = new HashMap();
        map.put("page", page+"");

        OkGoUtil.postRequest(getContext(), UrlConstants.REMIND_MSG, map, new JsonCallback<BaseModel<List<RemindMsgBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<RemindMsgBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        if(page == 1){
                            data.clear();
                        }
                        data.addAll(response.body().getData());
                        adapter.notifyDataSetChanged();
                        llNoData.setVisibility(data.size() == 0 ? View.VISIBLE : View.GONE);
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
                smlMsgList.finishRefresh();
                smlMsgList.finishLoadMore();
            }

            @Override
            public void onError(Response<BaseModel<List<RemindMsgBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                smlMsgList.finishRefresh();
                smlMsgList.finishLoadMore();
            }
        });
    }

    private void initRecyclerView() {
        data = new ArrayList<>();
        adapter = new RemindMsgAdapter();
        rvMsgList.setLayoutManager(new LinearLayoutManager(context));
        rvMsgList.setAdapter(adapter);
        adapter.setNewData(data);

        smlMsgList.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getMsg();
            }
        });

        smlMsgList.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page ++ ;
                getMsg();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}